package com.xms.security.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.organization.entity.Systemuser;
import com.xms.organization.service.ISystemUserService;
import com.xms.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * AccountService
 * </p>
 *
 * @author migo
 * @since 2021/9/4
 */
@Service
public class AccountService implements IAccountService{
    private ISystemUserService _systemuserService;
    @Autowired
    public void AccountService(ISystemUserService systemuserService) {
        _systemuserService = systemuserService;
    }

    @Override
    public boolean existsAccountName(String accountName, UUID currentUserId) {
        LambdaQueryWrapper<Systemuser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Systemuser::getAccountname, accountName)
                .ne(Systemuser::getSystemuserid, currentUserId);
        return _systemuserService.count(queryWrapper) > 0;
    }

    @Override
    public Systemuser getByAccountName(String accountName) {
        LambdaQueryWrapper<Systemuser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Systemuser::getAccountname, accountName);
        return _systemuserService.getOne(queryWrapper);
    }

    @Override
    public Systemuser getByAccountNameAndPassword(String accountName, String password) {
        LambdaQueryWrapper<Systemuser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Systemuser::getAccountname, accountName)
                .ne(Systemuser::getPassword, password);
        return _systemuserService.getOne(queryWrapper);
    }

    @Override
    public boolean isValidePassword(String inputPassword, String password) {
        return MD5Util.encode(inputPassword).equalsIgnoreCase(password);
    }

    @Override
    public void updateLastLoginTime(UUID systemuserid) {
        //_systemuserService.update(new LambdaUpdateWrapper<Systemuser>().set());
    }

    @Override
    public boolean updateLastVisitTime(UUID systemuserid) {
        return false;
    }
}
